<?php
/**
 * @title 1004. 最大连续1的个数 III
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param Integer[] $A
     * @param Integer $K
     * @return Integer
     */
    function longestOnes($A, $K){
        /**
         * @since 2021-03-02 长度
         */
        $len = count($A);
        // echo "len = {$len}", PHP_EOL;

        /**
         * @since 2021-03-02 位置
         */
        $pos = [];
        for ($i=0; $i<$len; $i++){
            if($A[$i] == 0){
                $pos[] = $i;
            } else {}
        }
        // return $pos;

        /**
         * @since 2021-03-02 连续1的长度
         */
        $poslen = count($pos);
        if($K >= $poslen){
            $max = $len;
            // echo "max={$max}", PHP_EOL;
        } else {
            $max = max($pos[$K], $len-$pos[$poslen-1]-1);
            // echo "i=0, max={$max}, poslen={$poslen}", PHP_EOL;
            for($i=1; $i<$poslen; $i++){
                /**
                 * @since 2021-03-02 如果超出数组范围, 则说明到底
                 */
                if($i+$K<$poslen){
                    $max = max($max, $pos[$i+$K]-$pos[$i-1]-1);
                } else {
                    $max = max($max, $len-$pos[$i-1]-1);
                    break;
                }
            }
        }

        /**
         * @return
         */
        return $max;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202102/2021.02.19.php
 */
$datas = [
    [[1,1,1,0,0,0,1,1,1,1,0], 2], // 6
    [[0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], 3], // 10
    [[0,0,1,1,1,0,0], 0], // 3
    [[1,1,1,0,0,0,1,1,1,1], 0], // 4
    [[0,0,0,0], 0], // 0
    [[1,1,0,0,0,0,1,1,0,1,0,1,1,0,1,0,1,0,0,0,1,0,1,1,1,1,1,0,1,1,1,0,0,1,1,1,0,0,1,0,0,0,0,1,1,1,0,1,1,1,0,1,1,1,1,0,1,0,0,0,0,0,0,1,1,1,1,1,1,0,1,0,0,1,0,1,0,1,1,0,0,0,0,0,1,1,1,1,0,1,0,0,1,1,0,1,0,0,0,0,1,1,0,0,0,0,0,1,0,0,0,1,1,1,1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,1,1,0,1,0,1,0,1,1,0,0,0,1,1,1,1,0,1,1,0,1,1,1,1,1,1,0,0,1,1,1,0,0,0,1,1,1,1,1,1,0,1,1,0,1,1,1,1,0,1,0,1,1,0,1,1,0,0,1,0,1,0,1,1,1,1,1,0,0,0,0,1,1,0,0,0,1,1,0,0,1,0,0,0,0,1,1,0,0,0,0,1,1,1,1,1,0,0,0,1,0,0,0,1,0,1,1,1,0,1,0,0,1,1,0,0,0,1,1,1,0,1,0,0,1,0,1,1,1,1,0,0,1,1,1,1,1,0,0,0,1,1,1,1,1,0,1,1,0,0,1,1,1,1,0,0,0,1,1,0,1,0,0,1,1,0,1,1,0,0,0,1,0,1,0,1,1,1,1,0,1,0,1,1,1,1,0,1,0,1,1,0,1,1,1,1,0,0,0,0,1,1,1,0,1,1,0,1,0,1,1,1,1,1,0,1,0,0,0,0,0,0,1,1,1,0,1,0,1,1,1,0,1,0,1,0,1,0,1,0,1,1,0,0,1,0,1,1,1,0,1,0,0,1,1,0,1,0,0,1,1,1,1,1,0,0,1,0,1,0,1,1,1,1,0,1,1,0,0,0,1,1,1,1,0,1,0,0,1,1,1,1,1,1,1,0,0,1,0,0,1,1,1,0,1,1], 289], // 653
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    // var_dump($data);

    $obj = new Solution();
    $result = $obj->longestOnes(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
